package com.only4play.httpapt.support.api.impl;

import com.google.gson.reflect.TypeToken;
import com.only4play.httpapt.common.json.OpHttpAptGsonBuilder;
import com.only4play.httpapt.support.api.JpCommentService;
import com.only4play.httpapt.support.api.JsonPlaceholderService;
import com.only4play.httpapt.support.bean.comments.JpCommentInfo;
import com.only4play.httpapt.support.bean.comments.JpCommentSearchResp;
import com.only4play.httpapt.support.constant.JsonPlaceholderConstants;
import lombok.RequiredArgsConstructor;

import java.util.List;

/**
 * @author liyuncong
 * @version 1.0
 * @file JpCommentServiceImpl
 * @brief JpCommentServiceImpl
 * @details JpCommentServiceImpl
 * @date 2024-08-16
 *
 * Edit History
 * ----------------------------------------------------------------------------
 * DATE                     NAME               DESCRIPTION
 * 2024-08-16               liyuncong          Created
 */

@RequiredArgsConstructor
public class JpCommentServiceImpl implements JpCommentService {
    private final JsonPlaceholderService mainService;

    @Override
    public JpCommentSearchResp getComments(Integer postId) throws Exception {
        String apiUrl = this.mainService.getConfigStorage().getApiUrl(JsonPlaceholderConstants.Comments.GET_COMMENTS);
        String queryParam = null;
        if (postId != null) {
            queryParam = "postId=" + postId;
        }
        String response = this.mainService.get(apiUrl, queryParam);
        List<JpCommentInfo> comments = OpHttpAptGsonBuilder.create().fromJson(response, new TypeToken<>() {
        });
        return new JpCommentSearchResp(comments);
    }
}
